const router = require('koa-router')()
const { sys_post: PostModel } = require('../../dbHelper/db').models
const { Op } = require('sequelize')
const { hasParamsFiled, toTreeByRecursion } = require('../../utils')

router.prefix('/post')

router.get('/getPostList', async(ctx) => {
  // 请求参数校验
  ctx.verifyParams({
    page_num: { type: 'string', required: true },
    page_size: { type: 'string', required: true }
  })

  const query = ctx.request.query

  const where = {}

  if (hasParamsFiled(query.post_code)) {
    where.post_code = query.post_code
  }

  if (hasParamsFiled(query.post_name)) {
    where.post_name = query.post_name
  }

  if (hasParamsFiled(query.status)) {
    where.status = query.status
  }

  const postList = await PostModel.findAndCountAll({
    where,
    offset: (query.page_num - 1) * query.page_size,
    limit: +query.page_size
  })

  ctx.body = {
    code: 0,
    data: postList,
    msg: 'ok'
  }
})

router.get('/getActivePost', async(ctx) => {
  const postList = await PostModel.findAll({
    where: {
      status: 0
    }
  })

  ctx.body = {
    code: 0,
    data: postList,
    msg: 'ok'
  }
})

router.post('/addPost', async(ctx) => {
  // 请求参数校验
  ctx.verifyParams({
    post_code: { type: 'string', required: true },
    post_name: { type: 'string', required: true },
    post_sort: { type: 'number', required: true },
    status: { type: 'enum', values: [0, 1] },
    remark: { type: 'string', required: false }
  })

  const { post_code, post_name, post_sort, status, remark } = ctx.request.body

  let objPost = await PostModel.findOne({
    where: {
      [Op.or]: [
        { post_code },
        { post_name }
      ]
    }
  })

  if (objPost) {
    ctx.body = {
      code: -1,
      msg: '岗位名称或岗位ID重复，创建失败！'
    }
  } else {
    objPost = await PostModel.create({
      post_code,
      post_name,
      post_sort,
      status,
      remark: remark || ''
    })

    ctx.body = {
      code: 0,
      msg: '创建成功！',
      data: objPost
    }
  }
})

router.put('/modifyPost', async(ctx) => {
  // 请求参数校验
  ctx.verifyParams({
    post_id: { type: 'number', required: true },
    post_code: { type: 'string', required: true },
    post_name: { type: 'string', required: true },
    post_sort: { type: 'number', required: true },
    status: { type: 'enum', values: [0, 1] }
  })

  const params = ctx.request.body

  const otherPost = await PostModel.findOne({
    where: {
      post_id: {
        [Op.ne]: params.post_id
      },
      [Op.or]: {
        post_code: params.post_code,
        post_name: params.post_name
      }
    }
  })

  if (otherPost) {
    ctx.body = {
      code: -1,
      msg: '岗位名称或岗位ID重复，不能修改！'
    }
  } else {
    await PostModel.update({
      post_code: params.post_code,
      post_name: params.post_name,
      post_sort: params.post_sort,
      status: params.status,
      remark: params.remark
    }, {
      where: {
        post_id: params.post_id
      }
    })

    ctx.body = {
      code: 0,
      msg: '修改成功！'
    }
  }
})

router.delete('/deletePost', (ctx) => {
  const query = ctx.request.query

  if (query.post_id === undefined) {
    ctx.body = {
      code: -1,
      msg: '参数错误'
    }
    return
  }

  PostModel.destroy({
    where: {
      post_id: query.post_id
    }
  })

  ctx.body = {
    code: 0,
    msg: '删除成功！'
  }
})

module.exports = router
